package com.redis.demo;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @Auther: Admin
 * @Date: 2021/3/3 09:30
 * @Description: dcl 当你模式
 */
public class JedisPoolUtil {

    private static volatile JedisPool instance = null;


    private JedisPoolUtil() {
    }


    public static JedisPool getInstance() {

        if (instance == null) {

            synchronized (JedisPoolUtil.class) {

                if (instance == null) {

                    JedisPoolConfig config = new JedisPoolConfig();
                    config.setMaxIdle(32);
                    config.setMaxTotal(1000);
                    config.setMaxWaitMillis(20 * 1000);
                    config.setTestOnBorrow(false);
                    config.setTestOnReturn(true);

                    instance = new JedisPool(config, "127.0.0.1", 6379);
                }

            }

        }

        return instance;

    }


    public static void release(JedisPool jedisPool, Jedis jedis) {


        if (jedis != null) {

            jedis.close();

        }


    }

    public static void main(String[] args) {

        JedisPool instance = JedisPoolUtil.getInstance();

        Jedis jedis = null;

        try {

            jedis = instance.getResource();

            jedis.set("k1", "v1");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JedisPoolUtil.release(instance, jedis);
        }


    }


}
